// ActionScript file
package com.app.business{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.app.factories.LoginFactory;
	import com.app.model.ModelLocator;
	import com.app.vo.VO;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class LoginDelegate implements IResponder{
		private var responder : IResponder; 
		private var service : HTTPService;  
		private var loginVO : VO;
		private var Param:String; 
		private var Act:String;
		private var Com:String;
	
		public function LoginDelegate( responder : IResponder ){
			this.service = ServiceLocator.getInstance().getHTTPService( "services" );
			this.responder = responder;
		}
		
		public function login( loginVO : VO ): void{
			Param = loginVO.username + "|" + loginVO.password;
            Act='doLogin';
            Com = 'login';
			var token:AsyncToken = service.send( {com:Com,act:Act,params:Param} );
	        token.addResponder(this); 
	        this.loginVO = loginVO; 
		}
		
		public function logout( loginVO : VO ):void{
		    Param = loginVO.username;
		    Act="doLogout";
		    Com="login";
		    //Alert.show(Act);
		    var token:AsyncToken=service.send( {com:Com,act:Act,params:Param} );
		    token.addResponder(this);
		    this.loginVO=loginVO;
		}
		
		public function result( data : Object ) : void{	
			var event:ResultEvent =  ResultEvent(data);
			//Alert.show(event.result as String);
			if(event.result != "false"){
				if(Act=="doLogin"){
			         this.loginVO.hakAkses=LoginFactory.buildPostArrayFromJSON(event.result as String);
				     responder.result(this.loginVO);
				}else if(Act=="doLogout"){
                     this.loginVO=LoginFactory.doLogout(event.result as String,this.loginVO);
				     responder.result(this.loginVO);
				}
			}else{
				responder.fault(null);
			}
		}
		
		public function fault( info : Object  ) : void
		{
			var err:FaultEvent = FaultEvent(info); 
			ModelLocator.getInstance().login.statusMessage = err.fault.faultString;
		}
	}
}